package com.example.controller;


import com.example.entity.AirspacePlan;
import com.example.service.AirspacePlanService;
import com.example.util.MicroResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @program: microservice
 * @description:
 * @author: Aprilies
 * @create: 2024-09-10 16:50
 **/

@Controller
public class AirspacePlanController {

    @Autowired
    private AirspacePlanService AirspacePlanService;

    private final Logger logger = LoggerFactory.getLogger(AirspacePlanController.class);

    //返回全体用户信息
    @GetMapping("/module2/info")
    public ResponseEntity getAllUsers() {
//        List<SysUser> userList = demoService.getAllUsers();
        return ResponseEntity.ok(MicroResponseUtils.success(AirspacePlanService.getAllUsers()));
    }

    //空域规划方案增加接口
    @PostMapping("/module2/save")
    public ResponseEntity saveUser(@RequestBody AirspacePlan user) {
        AirspacePlan savedUser = AirspacePlanService.isave(user);
        if (savedUser != null) {
            return ResponseEntity.ok(MicroResponseUtils.success(savedUser));
        }else {
            return  ResponseEntity.ok(MicroResponseUtils.error("Failed to change"));
        }
    }

    //仅仅状态更新
    @PostMapping("/module2/status")
    public ResponseEntity updateStatus(@RequestParam int id , @RequestParam Integer status)
    {
        Optional<AirspacePlan> result = AirspacePlanService.updateStatus(id, status);
        if(result.isPresent())
        {
            return ResponseEntity.ok(MicroResponseUtils.success(result));
        }else{
            return  ResponseEntity.ok(MicroResponseUtils.error("Failed to change"));
        }

    }

    //更新状态外其他信息
    @PostMapping("/module2/allinfo")
    public ResponseEntity updateall(@RequestParam int id, @RequestBody AirspacePlan updatedUser) {
        updatedUser.setId(id); // 确保用户ID正确
        Optional<AirspacePlan> result = AirspacePlanService.update(updatedUser);
        if(result.isPresent()){
            return ResponseEntity.ok(MicroResponseUtils.success(result));
        }else {
            return  ResponseEntity.ok(MicroResponseUtils.error("Failed to change"));
        }
    }

    //信息传递接口——传递信息的动作完成后向前端发送提示信息
    @PostMapping("/module2/transfer")
    public ResponseEntity<String> transferinfo(@RequestParam int id) {
        boolean success = AirspacePlanService.transferInfo(id);
        if (success) {
            return ResponseEntity.ok("信息传递成功");
        }else{
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("信息传递失败");
        }
//        if (success) {
//        return ResponseEntity.ok("信息传递成功");
//        } else {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("信息传递失败");
//        }
    }

    @PostMapping("/module2/search")
    @ResponseBody
    public ResponseEntity search(
            @RequestParam(required = false) String keyword, // 新增的关键字参数
            @RequestParam(required = false) String sort,
            @RequestParam(required = false) boolean order) {

        //创建Logger对象
        logger.info("Received search request - keyword: {}, sort: {}, order: {}",keyword,sort,order);
        // 调用服务层方法，并传递关键字
//        SearchResult pageInfo = demoService.niubi(keyword, sort, order);
//        // 创建 MicroResponse 对象
//        MicroResponse<SearchResult> response = MicroResponseUtils.success(pageInfo);
//        // 返回 ResponseEntity 对象
//        return ResponseEntity.status(HttpStatus.OK).body(response);
        return ResponseEntity.ok(MicroResponseUtils.success(AirspacePlanService.conditionalSearch(keyword,sort,order)));
//        // 调用服务层方法，并传递关键字
//        SearchResult pageInfo = demoService.keyser(keyword, sort, order);
//        return ResponseEntity.ok(MicroResponseUtils.success(pageInfo));
    }



    //很重要
//    @GetMapping("/module2/search")
//    @ResponseBody
//    public ResponseEntity search(
//                                     @RequestParam(required = false) String keyword, // 新增的关键字参数
//                                     @RequestParam(required = false) String sort,
//                                     @RequestParam(required = false) boolean order) {
//        // 调用服务层方法，并传递关键字
//        List<SysUser> pageinfo = demoService.keyser( keyword, sort, order);
//        return ResponseEntity.ok(MicroResponseUtils.success(pageinfo));
//    }
//    @PostMapping("module2/keyse")
//    public ResponseEntity pagesort(
//            @RequestParam Integer current,
//            @RequestParam Integer size,
//            @RequestBody SysCondition condition,
//            @RequestParam(required = false,defaultValue = "id") String sort,
//            @RequestParam(required = false,defaultValue = "asc") String order
//            ){
//        MicroPage<SysUser> pagesort = demoService.keyser(current, size, condition, sort, order);
//        return ResponseEntity.ok(MicroResponseUtils.success(pagesort));
//    }

    //关键字搜索接口(包含排序功能)
    //    @GetMapping("/module2/key")
    //    @ResponseBody
    //    public List<SysUser> searchUsers(@RequestParam("keyword") String keyword,
    //                                     @RequestParam(value = "sortby", defaultValue = "id") String sortBy,
    //                                     @RequestParam(value = "order", defaultValue = "asc") String order) {
    ////        MicroPage<SysUser> searchusers = demoService.searchUsersByKeyword(keyword, sortBy, order);
    ////        return ResponseEntity.ok(MicroResponseUtils.success(searchusers));
    //        return demoService.searchUsersByKeyword(keyword , sortBy , order);
    //    }


    //单状态更新
//    @PostMapping("/module2/statusanother")
//    public ResponseEntity<Optional<SysUser>> updateone(@RequestParam int id, @RequestBody SysUser updatedUser) {
//        updatedUser.setId(id); // 确保用户ID正确
//        Optional<SysUser> result = demoService.updateOnly(updatedUser);
//        return ResponseEntity.ok(result);
//    }


    //    // 增加分页功能(空域规划方案分页查询接口)
    //    @PostMapping("/page")
    //    public ResponseEntity page(@RequestParam Integer current, @RequestParam Integer size, @RequestBody SysUser condition) {
    //        MicroPage<SysUser> page = demoService.page(current, size, condition);
    //        return ResponseEntity.ok(MicroResponseUtils.success(page));
    //    }

}

